using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Threading;

namespace RandomLogfileCreator
{
    class Program
    {
        static long amountOfEntries = 20000L; // ca. 2,8 MB

        static string path = @"c:\testLogFile.log";

        static int pauseMillis = 0;

        static void Main(string[] args)
        {
            string[] allTypes = new string[5];
            allTypes[0] = "FATAL";
            allTypes[1] = "ERROR";
            allTypes[2] = "WARN";
            allTypes[3] = "DEBUG";
            allTypes[4] = "INFO";
            string[] allPackages = new string[6];
            allPackages[0] = "de._test.controls.GEmomentan";
            allPackages[1] = "de.softenergy.wiscom.control.ge.GEDatagramReceiver";
            allPackages[2] = "at.bachmann.sys.common.smi.RpcCall";
            allPackages[3] = "java.io.ByteArrayInputStream";
            allPackages[4] = "de.repower.avs.dacc";
            allPackages[5] = "org.apache.axis2.util.Loader";
            string[] allMessages = new string[8];
            allMessages[0] = "Dialup: 006753124751";
            allMessages[1] = "Fetching data for plant nr 1  -> 1 of 1";
            allMessages[2] = "GE: The entered serial number is wrong: <null> !";
            allMessages[3] = "GE: No data available for plant number <15811>. Maybe wrong number in park specified.\r\nAvailable numbers are:  4  |  5  |  6  |  8  |  9  |  11  |  12  |  100  |  ";
            allMessages[4] = "Trying to find [org/apache/axis2/deployment/axis2_default.xml] using sun.misc.Launcher$AppClassLoader@133056f class loader.";
            allMessages[5] = "COMMENT: [\r\n  ~ Licensed to the Apache Software Foundation (ASF) under one\r\n  ~ or more contributor license agreements. See the NOTICE file]";
            allMessages[6] = "COMMENT: [During a fault, stacktrace can be sent with the fault message. The following flag will control ]";
            allMessages[7] = "START_ELEMENT: parameter:parameter";
            
            Random rand = new Random();

            Console.WriteLine("Creating random filled log file...");

            StreamWriter bufWriter = null;
            try
            {
                bufWriter = new StreamWriter(path);
                StringBuilder currentEntry = null;
                DateTime cal = DateTime.Now;

                Console.WriteLine();
                //Console.Write("Entries created:  " + amountOfEntries + "/0");
                for (int i = 0; i < amountOfEntries; i++)
                {
                    currentEntry = new StringBuilder();
                    cal = cal.AddSeconds(rand.Next(5, 500));
                    currentEntry.Append(cal.ToString("dd.MM.yyyy HH:mm:ss"));
                    int randType = rand.Next(allTypes.Length);
                    currentEntry.Append(" [" + allTypes[randType] + "] ");
                    int randPackage = rand.Next(allPackages.Length);
                    currentEntry.Append(allPackages[randPackage]);
                    currentEntry.Append(" - ");
                    int randMessage = rand.Next(allMessages.Length);
                    currentEntry.Append(allMessages[randMessage]);
                    bufWriter.WriteLine(currentEntry.ToString());

                    Console.WriteLine(currentEntry.ToString());
                    Console.WriteLine();

                    bufWriter.Flush();

                    Thread.Sleep(pauseMillis);

                    //if (i>0 && (i % 10 == 0))
                    //{
                    //    string entriesUntilNow = (i - 10).ToString();
                    //    Console.CursorLeft -= entriesUntilNow.Length;
                    //    Console.Write(i.ToString());
                    //}
                }
                Console.CursorLeft -= (amountOfEntries - 10).ToString().Length;
                Console.Write(amountOfEntries.ToString());
                Console.WriteLine();
                Console.WriteLine("Finished, press any key.");
                Console.ReadKey();
            }
            catch (IOException e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                if (bufWriter != null)
                {
                    try
                    {
                        bufWriter.Close();
                    }
                    catch (IOException e)
                    {
                        Console.WriteLine(e.Message);
                    }
                }
            }
        }
    }
}
